package com.xiaolee.app1.config;

import com.xiaolee.app1.security.LogInterceptor;
import com.xiaolee.app1.security.LoginFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import javax.servlet.Filter;

/**
 * web安全配置
 *
 * @author lizhongcheng@didiglobal.com
 * @version 1.0
 * @date 2021-06-04 15:13:52
 * @description
 */
@Configuration
public class WebSecurityConfig implements WebMvcConfigurer {
    @Resource
    private LoginFilter loginFilter;
    @Resource
    private LogInterceptor logInterceptor;

    @Bean
    public FilterRegistrationBean<Filter> loginFilterRegisterBean() {
        FilterRegistrationBean<Filter> filterBean = new FilterRegistrationBean<>();
        filterBean.setFilter(loginFilter);
        filterBean.addUrlPatterns("/*");
        filterBean.setAsyncSupported(true);
        filterBean.setEnabled(true);

        return filterBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(logInterceptor).addPathPatterns("/**");
    }
}
